package com.beemans.topon.demo.ui.fragments

import android.util.Log
import com.anythink.core.api.ATAdInfo
import com.anythink.core.api.AdError
import com.beemans.common.ui.fragments.CommonFragment
import com.beemans.common.utils.ToastUtils
import com.beemans.topon.ads2.AdvertManager
import com.beemans.topon.ads2.RewardAdvert
import com.beemans.topon.ads2.RewardAdvertCallback
import com.beemans.topon.ads2.RewardAdvertConfig
import com.beemans.topon.demo.R
import com.beemans.topon.demo.constant.Constant
import com.beemans.topon.demo.databinding.FragmentRewardAdBinding
import com.tiamosu.databinding.delegate.lazyDataBindingOrNull
import com.tiamosu.databinding.page.DataBindingConfig
import com.tiamosu.fly.ext.clickNoRepeat

/**
 * @author tiamosu
 * @date 2020/7/7.
 */
class RewardAdFragment : CommonFragment() {
    private val dataBinding: FragmentRewardAdBinding? by lazyDataBindingOrNull { }
//    private val rewardAdConfig by lazy { TpRewardAd.TpRewardAdConfig(Constant.REWARD_ID) }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_reward_ad)
    }

    override fun initEvent() {
        // 加载广告
        dataBinding?.rewardAdBtnPreload?.clickNoRepeat {
            preloadAdvert()

//            val config = rewardAdConfig.deepClone()?.apply { isOnlyPreload = true }
//                ?: return@setOnClickListener
//            TpRewardAd.start(this, config) {
//                onAdLoaded {
//                    val cache = TopOn.getRewardAdCaches(this@RewardAdFragment, config.placementId)
//                    Log.e("susu2", "cache:${cache.size}")
//                }
//            }
        }

        // 展示广告
        dataBinding?.rewardAdBtnShow?.clickNoRepeat {
            showAdvert()

//            TpRewardAd.start(this, rewardAdConfig) {
//                onAdClose { _, isReward, isAgainReward ->
//                    Log.e("susu2", "isReward:$isReward   isAgainReward:$isAgainReward")
//                }
//            }
        }
    }

    /**
     * 加载广告
     */
    private fun preloadAdvert() {
        val advertConfig = RewardAdvertConfig(Constant.REWARD_ID)

        AdvertManager.loadRewardAdvert(this, advertConfig, object : RewardAdvertCallback() {
            override fun onAdvertLoaded(rewardAdvert: RewardAdvert) {
                printLog("广告加载成功")
            }

            override fun onAdvertLoadFail(rewardAdvert: RewardAdvert, adError: AdError?) {
                printLog("广告加载失败")
            }
        })
    }

    /**
     * 展示广告
     */
    private fun showAdvert() {
        val advertConfig = RewardAdvertConfig(Constant.REWARD_ID)
        advertConfig.isShowAfterLoaded = true
        advertConfig.isPreloadAdvert = true
        advertConfig.isPreloadWithoutReady = true

        AdvertManager.loadRewardAdvert(this, advertConfig, object : RewardAdvertCallback() {
            override fun onAdvertLoaded(rewardAdvert: RewardAdvert) {
                printLog("广告加载成功")
            }

            override fun onAdvertLoadFail(rewardAdvert: RewardAdvert, adError: AdError?) {
                printLog("广告加载失败")
            }

            override fun onAdvertShow(rewardAdvert: RewardAdvert, atAdInfo: ATAdInfo?) {
                printLog("广告展示")
            }

            override fun onAdvertClicked(rewardAdvert: RewardAdvert, atAdInfo: ATAdInfo?) {
                printLog("广告点击")
            }

            override fun onAdvertClose(rewardAdvert: RewardAdvert, atAdInfo: ATAdInfo?): Boolean {
                printLog("广告关闭")
                return true
            }

            override fun onAdvertReward(rewardAdvert: RewardAdvert, atAdInfo: ATAdInfo?) {
                printLog("广告下发奖励")
            }
        })
    }

    private fun printLog(msg: String) {
        Log.e("susu", msg)
        ToastUtils.showShort(msg)
    }

    override fun doBusiness() {
    }
}